home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / wpjv1n2.zip / LINKLIST.ZIP / LINKLIST.C next >
C/C++ Source or Header  |  1993-01-30  |  6KB  |  219 lines

  1. /*  LINKLIST.C
  2.     by Mike Wallace
  3.     Windows Programmer's Journal
  4.     Volume 1 Number 1
  5.     Copyright 1992
  6. */
  7.  
  8. /* linked list windows application */
  9. #include <windows.h>
  10. #include <stdio.h>
  11. #include "linklist.h"
  12.  
  13. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  14.                     LPSTR lpszCmdLine, int nCmdShow)
  15. {
  16.  
  17.         HWND hWnd;
  18.         MSG msg;
  19.         WNDCLASS wndclass;
  20.  
  21.         ghInstance = hInstance;
  22.  
  23.         if(!hPrevInstance)
  24.         {
  25.                 wndclass.style = CS_HREDRAW | CS_VREDRAW;
  26.                 wndclass.lpfnWndProc = WndProc;
  27.                 wndclass.cbClsExtra = 0;
  28.                 wndclass.cbWndExtra = 0;
  29.                 wndclass.hInstance = hInstance;
  30.                 wndclass.hIcon = LoadIcon (hInstance, ProgName);
  31.                 wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
  32.                 wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  33.                 wndclass.lpszMenuName = ProgName;
  34.                 wndclass.lpszClassName = ProgName;
  35.  
  36.                 if (!RegisterClass (&wndclass)) return FALSE;
  37.  
  38.         }
  39.  
  40.         hWnd = CreateWindow (
  41.                 ProgName,
  42.                 ProgName,
  43.                 WS_OVERLAPPEDWINDOW,
  44.                 CW_USEDEFAULT,
  45.                 CW_USEDEFAULT,
  46.                 CW_USEDEFAULT,
  47.                 CW_USEDEFAULT,
  48.                 NULL,
  49.                 NULL,
  50.                 hInstance,
  51.                 NULL);
  52.  
  53.         ShowWindow (hWnd, nCmdShow);
  54.         UpdateWindow (hWnd);
  55.  
  56.         while (GetMessage (&msg, NULL, 0, 0))
  57.         {
  58.                 TranslateMessage (&msg);
  59.                 DispatchMessage (&msg);
  60.         }
  61.         return msg.wParam;
  62. } /* WinMain */
  63.  
  64. long FAR PASCAL WndProc (HWND hWnd, unsigned Msg, WORD wParam, LONG lParam)
  65. {
  66.  
  67.         switch (Msg)
  68.         {
  69.                 case WM_COMMAND:
  70.                         switch (wParam)
  71.                         {
  72.                         case IDM_RUN:
  73.                                 MakeList (hWnd);
  74.                      MessageBox (hWnd, "Done", "Hey", MB_OK);  
  75.                                 break;
  76.                         
  77.                         case IDM_QUIT:
  78.                                 DestroyWindow (hWnd);
  79.                                 break;
  80.                         }
  81.                         break;
  82.                 case WM_DESTROY:
  83.                         PostQuitMessage (0);
  84.                         break;
  85.                 default:
  86.                         return DefWindowProc (hWnd, Msg, wParam, lParam);
  87.         }
  88.         return (0L);
  89. } /* WndProc */
  90.  
  91. /*******************************************************/
  92. /* Build a linked list, store the numbers 1-10, go     */
  93. /* through the list and print out the values, and then */
  94. /* go through the list again and free the nodes        */
  95. /*******************************************************/
  96.  
  97. void FAR PASCAL MakeList (HWND hWnd)
  98. {
  99.     /* local variables */
  100.     short           i;
  101.     static HANDLE   hMem, hMem2, hMem3;
  102.  
  103.     /* structure of each node in linked list */
  104.     typedef struct _listmarker {
  105.  
  106.         short   value;
  107.         HANDLE  next;
  108.  
  109.     } ListMarker;
  110.  
  111.     /* declare a far pointer to the above structure */
  112.     typedef ListMarker FAR *LISTMARKER;
  113.  
  114.     LISTMARKER tailPos, tempPos, headPos;
  115.  
  116.     /* pointer to output file */
  117.     FILE    *fp;
  118.  
  119.     /* Open output file for writing */
  120.     fp= fopen("data.out", "w");
  121.  
  122.     /* Build initial linked list of the numbers 1 to 10 */
  123.     if((hMem= GlobalAlloc(GMEM_DISCARDABLE | GMEM_MOVEABLE, 
  124.         sizeof(ListMarker))) == NULL) {
  125.  
  126.         /* Not enough memory, so beep, show a message and return */
  127.         MessageBeep(0);
  128.         MessageBox(hWnd, "Out of allocation memory!", "ERROR", MB_OK);
  129.  
  130.         return;
  131.             
  132.     }
  133.  
  134.     /* Lock the first node, save a value, set next to NULL and unlock */
  135.     tailPos= headPos= (LISTMARKER) GlobalLock(hMem);
  136.     headPos->value= 1;
  137.     headPos->next= NULL;
  138.     
  139.     for (i=2; i < 11; i++) {
  140.             
  141.         /* setup index lookup lists */
  142.         if((tailPos->next= GlobalAlloc(GMEM_DISCARDABLE | GMEM_MOVEABLE, 
  143.             sizeof(ListMarker))) == NULL) {
  144.     
  145.             MessageBeep(0);
  146.             MessageBox(hWnd, "Out of allocation memory!", "ERROR",
  147.                             MB_OK);
  148.  
  149.             return;
  150.                     
  151.         }  /* If - End */
  152.  
  153.         /* Lock the next node, save the value, and set its next to NULL */
  154.         hMem2= tailPos->next;
  155.         tempPos= (LISTMARKER) GlobalLock(hMem2);
  156.         tailPos= tempPos;
  157.         tailPos->value= i;
  158.         tailPos->next= NULL;
  159.  
  160.         GlobalUnlock(hMem2);
  161.                     
  162.     }  /* While - End */
  163.  
  164.     GlobalUnlock(hMem);
  165.  
  166.     /* Lock the first node and write out its "value" */
  167.     tailPos= headPos= (LISTMARKER) GlobalLock(hMem);
  168.     fprintf(fp, "%d\n", tailPos->value);
  169.  
  170.     /* Save the handle to the next node */
  171.     hMem2= tailPos->next;
  172.  
  173.     /* Unlock the 1st node */
  174.     GlobalUnlock(hMem);
  175.  
  176.     /* Go through list and print out "value" until no more nodes */
  177.     while (hMem2 != NULL) {
  178.         
  179.         /* Lock the next node and save to tailPos */
  180.         tempPos= (LISTMARKER) GlobalLock(hMem2);
  181.         tailPos= tempPos;
  182.  
  183.         fprintf(fp, "%d\n", tailPos->value);
  184.  
  185.         /* Get the handle to the next node and then unlock the current one */
  186.         hMem3= tailPos->next;
  187.         GlobalUnlock(hMem2);
  188.         hMem2= hMem3;
  189.                             
  190.     } /* While - End */
  191.  
  192.     /* Close the output file */
  193.     fclose(fp);                    
  194.  
  195.     /* free nodes in the list */
  196.     tempPos= (LISTMARKER) GlobalLock(hMem);
  197.     hMem2= tempPos->next;
  198.     tempPos= (LISTMARKER) GlobalLock(tempPos->next);
  199.     GlobalUnlock(hMem);
  200.     GlobalFree(hMem);
  201.  
  202.     while(tempPos->next != NULL) {
  203.  
  204.        hMem3= tempPos->next;
  205.        tempPos= (LISTMARKER) GlobalLock(tempPos->next);
  206.        GlobalUnlock(hMem2);
  207.        GlobalFree(hMem2);
  208.        hMem2=hMem3;
  209.  
  210.     }
  211.  
  212.     GlobalUnlock(hMem2);
  213.     GlobalFree(hMem2);
  214.  
  215.     return;
  216.  
  217. } /* MakeList */
  218.  
  219.